package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.ShareTaskData;
import com.burgeon.backend.EnterpriseWeChat.entity.ShareTaskGoods;
import com.burgeon.backend.EnterpriseWeChat.mappers.EWShareTaskMapper;
import com.burgeon.backend.EnterpriseWeChat.service.EWSHareTaskService;
import com.burgeon.backend.EnterpriseWeChat.service.ex.UpDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class EWSHareTaskServiceImpl implements EWSHareTaskService {

    @Autowired
    private EWShareTaskMapper ewShareTaskMapper;

    /**
     * 导购下载图片
     * @param empid
     * @param gid
     * @param token
     * @return
     */
    @Override
    @Transactional
    public Integer insertShareTask(String empid, String gid, String token,String storeId,String empName,String storeName) {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String shareDate=format.format(date);
        String seDate=format2.format(date);
        String id = ewShareTaskMapper.getShareTaskByGid(empid,token,gid,format2.format(date));
        if(id==null){
            //新增导购的下载
            Integer row = ewShareTaskMapper.insertShareTask(empid,gid,"1",shareDate,token,
                    "0","0",storeId, empName,storeName);
            if(row == 0){
                throw new UpDataException("分享任务错误");
            }
            //修改导购的分享数据
            ShareTaskData shareTaskData = ewShareTaskMapper.getEmpRate(empid,token,seDate);
            //查看配置
            int todayCount = ewShareTaskMapper.getShareCount(token);
            //如果没有数据就新增
            if(shareTaskData==null){
                //完成率
                DecimalFormat df = new DecimalFormat("###.##");
                String rate ;
                String s=df.format(row*100D/todayCount);
                rate = s+"%";
                ewShareTaskMapper.addShareTaskData(empid,row.toString(),rate,token,shareDate,shareDate,row.toString(),seDate);
            }else {

                DecimalFormat df = new DecimalFormat("###.##");
                int count = row+Integer.parseInt(shareTaskData.getCount());
                System.err.println(count);
                //是否是同一天
//                if(!shareDate.substring(0,8).equals(shareTaskData.getModDate().substring(0,8))){
//                    String rate ;
//                    String s=df.format(row*100D/todayCount);
//                    rate = s+"%";
//                    //不是同一天修改本日，和昨日的数据
//                    ewShareTaskMapper.addShareTaskData(empid,row.toString(),rate,token,shareDate,shareDate,row.toString());
////                    ewShareTaskMapper.updateShareDataByEmpId(empid,row.toString(),rate,token,shareDate,
////                            Integer.toString(count));
//                }else {
                    //今日完成率
                    String rate ;
                    String s=df.format((row+Integer.parseInt(shareTaskData.getToday()))*100D/todayCount);
                    rate = s+"%";
                    int today = row+Integer.parseInt(shareTaskData.getToday());

                    //同一天只修改今日的数据
                    ewShareTaskMapper.updateShareDataByEmpId(empid, Integer.toString(today),rate, token,shareDate,
                            Integer.toString(count));
//                }

            }
            return row;
        }else {
            //统一导购，同一商品，同一天只更新时间
            Integer row = ewShareTaskMapper.updateShareData(id,shareDate);
            return row;
        }

    }

    @Override
    public ShareTaskData getEmpRate(String empId, String token, String shareDate) {
        if(null ==shareDate||shareDate.isEmpty()){
            SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
            Date date = new Date();
            shareDate=format2.format(date);
        }

        ShareTaskData shareTaskData = ewShareTaskMapper.getEmpRate(empId,token,shareDate);
        //查看配置次数
        int count = ewShareTaskMapper.getShareCount(token);
        int doneCount = 0;//已完成
        int undoneCount = 0;//未完成
        if(shareTaskData ==null){
              undoneCount=count;
            ShareTaskData shareTaskData1 = new ShareTaskData();
            shareTaskData1.setWaitCount(Integer.toString(count));
            shareTaskData1.setDoneCount(Integer.toString(doneCount));
            shareTaskData1.setUndoneCount(Integer.toString(undoneCount));
            shareTaskData1.setTodayRate("0%");
            return shareTaskData1;

        }else {
             doneCount =Integer.parseInt(shareTaskData.getToday());//已完成
//             undoneCount=0;//未完成
            if(count >=doneCount){
                undoneCount = count-doneCount;
            }
            shareTaskData.setWaitCount(Integer.toString(count));
            shareTaskData.setDoneCount(Integer.toString(doneCount));
            shareTaskData.setUndoneCount(Integer.toString(undoneCount));
        }

        return shareTaskData;
    }

    @Override
    public List<ShareTaskGoods> getShareTaskList(String empId, String token, String shareDate) {
        if(null ==shareDate||shareDate.isEmpty()){
            SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
            Date date = new Date();
             shareDate=format2.format(date);
        }
        List<ShareTaskGoods> shareTaskGoods = ewShareTaskMapper.getShareTaskList(empId, token,shareDate);
        return shareTaskGoods;
    }


}
